﻿using QuanLyThuVien.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyThuVien.Supporter;
using System.Data.SqlClient;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng CT_PHIEUMUON
    //----------------------------------------------------------------------
    public class DALCT_PhieuMuon
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALCT_PhieuMuon()
        { }

        //----------------------------------------------------------------------
        //Desc: chuyển dòng dữ liệu thành CT_PhieuMuon
        //----------------------------------------------------------------------
        public DTOCT_PhieuMuon ConvertToCT_PhieuMuon(DataRow row)
        {
            DTOCT_PhieuMuon ctpmu = new DTOCT_PhieuMuon();
            ctpmu.MaPhieuMuon = row["MaPhieuMuon"].ToString();
            ctpmu.MaCuonSach = row["MaCuonSach"].ToString();
            return ctpmu;
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách CT_PhieuMuon trong Phiếu Mượn
        //----------------------------------------------------------------------
        public List<DTOCT_PhieuMuon> LayDanhSachCT_PhieuMuon(string maphieumuon)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spPhieuMuon_LayCT_PhieuMuon";
            SqlParameter sqlprMaPhieuMuon = new SqlParameter("@MaPhieuMuon", SqlDbType.VarChar, 15) { Value = maphieumuon };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprMaPhieuMuon);
            if (data == null)
                return null;
            List<DTOCT_PhieuMuon> list = new List<DTOCT_PhieuMuon>();
            foreach (DataRow row in data.Rows)
                list.Add(ConvertToCT_PhieuMuon(row));
            return list;
        }

        //----------------------------------------------------------------------
        //Desc: Cập nhật CT_PhieuMuon
        //----------------------------------------------------------------------
        public bool CapNhat(DTOCT_PhieuMuon ctpmu)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spCT_PhieuMuon_CapNhatCTPM";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter slqprMaPhieuMuon = new SqlParameter("@MaPhieuMuon", SqlDbType.VarChar, 15) { Value = ctpmu.MaPhieuMuon };
            SqlParameter slqprMaCuonSach = new SqlParameter("@MaCuonSach", SqlDbType.VarChar, 15) { Value = ctpmu.MaCuonSach };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, slqprMaPhieuMuon, slqprMaCuonSach);
        }
    }
}
